package com.haha;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

// KeyConfigDialog.java 修改后
public class KeyConfigDialog extends JDialog {
    private final Map<String, JTextField> bindFields = new HashMap<>();
    private final String currentProfession;

    public KeyConfigDialog(Frame parent, String profession) {
        super(parent, "按键配置 - " + profession, true);
        this.currentProfession = profession;
        setSize(400, 300);
        setLocationRelativeTo(parent);

        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));

        // 根据职业加载对应的技能列表
        String[] skills = getSkillsForProfession(profession);
        for (String skill : skills) {
            JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            JLabel skillLabel = new JLabel(skill);
            JTextField keyField = new JTextField(ConfigManager.getKeyBind(profession, skill), 10);
            keyField.setEditable(false);
            keyField.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    startKeyCapture(keyField, skill);
                }
            });
            bindFields.put(skill, keyField);
            rowPanel.add(skillLabel);
            rowPanel.add(keyField);
            contentPanel.add(rowPanel);
        }

        JButton saveBtn = new JButton("保存配置");
        saveBtn.addActionListener(e -> {
            for (String skill : skills) {
                JTextField field = bindFields.get(skill);
                ConfigManager.setKeyBind(currentProfession, skill, field.getText());
            }
            ConfigManager.saveConfig();
            dispose();
        });

        mainPanel.add(new JScrollPane(contentPanel), BorderLayout.CENTER);
        mainPanel.add(saveBtn, BorderLayout.SOUTH);
        add(mainPanel);
    }

    private String[] getSkillsForProfession(String profession) {
        switch (profession) {
            case "德鲁伊-野德-WLK":
                return new String[]{"野蛮咆哮", "裂伤（豹）", "横扫", "斜掠", "割裂", "撕碎", "精灵之火（野性）"};
            case "战士-狂暴-WLK":
                return new String[]{"英勇打击", "顺劈斩", "嗜血"};
            // 添加其他职业配置...
            default:
                return new String[0];
        }
    }

    private void startKeyCapture(JTextField field, String skillName) {
        field.setText("按下按键...");
        field.requestFocus();

        KeyAdapter listener = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                String keyText = KeyEvent.getKeyText(e.getKeyCode());
                field.setText(keyText);
                ConfigManager.setKeyBind(currentProfession, skillName, keyText);
                field.removeKeyListener(this);
            }
        };

        field.addKeyListener(listener);
    }
}
